#!groovy

@Library('jenkinslibrary@master') _

//func from shareibrary
def build = new org.devops.build()
def tools = new org.devops.tools()
def deploy = new org.devops.deploy()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()

//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String deployHosts = "${env.deployHosts}"

String userEmail = "jiaojiao314@qq.com"


// if ("${runOpts}" == "GitlabPush"){
//     branchName = branch - "refs/heads/"
    
//     currentBuild.description = "Trigger'(触发者)'来自by ${userName}'(用户)'${branchName}'(分支)'"
//     //gitlab.ChangeCommitStatus(projectId,commitSha,"running")
//     env.runOpts = "GitlabPush"
    
// } 
 parameters {
    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
  }



//pipeline
pipeline{
    agent { node { label "master"}}

    environment{
        PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/jdk1.8.0_251/bin"
    }
    stages{


        stage("SCM"){

            steps{
                script{ 
                    tools.PrintMes("拉取分支为","green") 

                    // println("${branchName}") 
                         
                    tools.PrintMes("获取代码","green") 
                    checkout([$class: 'GitSCM', branches: [[name:"${BRANCH}"]], 
                                    doGenerateSubmoduleConfigurations: false, 
                                    extensions: [], 
                                    submoduleCfg: [],
                                    userRemoteConfigs: [[credentialsId: 'b2ed3d52-d843-4516-8500-4338856e27ac', url: "${srcUrl}"]]])
                }
            }
     
        }


        stage("Build"){
            
            steps{
                
                script{
                
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)
                   // artifactory.main(buildType,buildShell)
                   // artifactory.PushArtifact()
                    
                    //上传制品
                    //nexus.main("nexus")
                    //sh 'ansible -m ping hzjt183'
                    //发布制品
                    //sh " wget ${artifactUrl} && ls "
                    
                    
                    //deploy.SaltDeploy("${deployHosts}","test.ping")
                    //deploy.AnsibleDeploy("${deployHosts}","-m ping ")
                    deploy.AnsibleDeploy("${deployHosts}","-m shell  -a 'cd /var/lib/jenkins/workspace/流水线-test-1/target && java -jar my-app-1.1-SNAPSHOT.jar' ")
                }
            }
        }

    }

     post {
        always{
            script{
                println("always")
            }
        }
    
        
        success{
            script{
                println("success")
                // if ("${runOpts}" == "GitlabPush"){
                //     gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                // }
                toemail.Email("流水线成功",userEmail)

                    // dingtalk (
                    //     robot: 'cccbf709-d413-40d3-802c-c8fcc5174c65',
                    //     type: 'MARKDOWN',
                    //     title: '你有新的消息，请注意查收',
                    //     text: [
                    //         '# Jenkins流水线通知',
                    //         '## hbhx-ui构建成功',
                    //         '![jenkis](https://cdn.nlark.com/yuque/0/2020/png/671316/1594959749352-assets/web-upload/839227cb-07d9-47f0-81ba-486a56b9ec33.png)',
                    //         '[点击查看流水线详情](http://131.2.101.101:8080/job/%E6%B5%81%E6%B0%B4%E7%BA%BF-test-1/)',
                    //     ],
                    //     at: [
                    //       '13931883791'
                    //     ]
                    // )

            }
        
        }
        failure{
            script{
                println("failure")
                // if ("${runOpts}" == "GitlabPush"){
                //     gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
                // }
                toemail.Email("流水线失败了！",userEmail)
            }
        }
        
    //     aborted{
    //         script{
    //             println("aborted")
    //             if ("${runOpts}" == "GitlabPush"){
    //                 gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
    //             }
    //            toemail.Email("流水线被取消了！",userEmail)
    //         }
        
    //     }
    
    }   
}